package com.roncoo.education.course.service.biz;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.enums.StatusIdEnum;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.course.dao.ExperimentDao;
import com.roncoo.education.course.dao.UserExperimentDao;
import com.roncoo.education.course.dao.impl.mapper.entity.Experiment;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentWithBLOBs;
import com.roncoo.education.course.dao.impl.mapper.entity.UserExperiment;
import com.roncoo.education.course.dao.impl.mapper.entity.UserExperimentExample;
import com.roncoo.education.course.service.api.biz.ApiOptionsBiz;
import com.roncoo.education.course.service.biz.req.ExperimentReq;
import com.roncoo.education.course.service.biz.resp.CourseLecturerResp;
import com.roncoo.education.course.service.biz.resp.ExperimentResp;
import com.roncoo.education.user.feign.interfaces.IFeignLecturer;
import com.roncoo.education.user.feign.interfaces.vo.LecturerViewVO;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * AUTH-实验信息
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class ExperimentBiz extends BaseBiz {

    @NotNull
    private final ExperimentDao dao;
    @NotNull
    private final ApiOptionsBiz optionsBiz;
    @NotNull
    private final UserExperimentDao userExperimentDao;
    @NotNull
    private final ExperimentDao experimentDao;    
    @NotNull
    private final IFeignLecturer feignLecturer;
    
    /**
     * 实验查看接口
     *
     * @param req
     * @param userId
     * @return
     */
    public Result<ExperimentResp> view(ExperimentReq req, String token, Long userId) {
    	Experiment experiment = dao.getById(req.getId());
        if (experiment == null) {
            return Result.error("找不到该实验信息");
        }
        if (!experiment.getStatusId().equals(StatusIdEnum.YES.getCode())) {
            return Result.error("该实验信息已被禁用");
        }
        
        if (userId != null) {
        	StringBuffer sb = new StringBuffer();
        	sb.append(experiment.getWangzhi());
        	sb.append("?ticket=" + token);
        	experiment.setWangzhi(sb.toString());
        }
        
        experiment = optionsBiz.setOptionNameByOptionValue(experiment);
        
        ExperimentResp experimentResp = BeanUtil.copyProperties(experiment, ExperimentResp.class);
        // 获取讲师信息
        LecturerViewVO lecturerViewVO = feignLecturer.getById(experiment.getZuozheId());
        if (ObjectUtil.isNotEmpty(lecturerViewVO)) {
            experimentResp.setLecturerResp(BeanUtil.copyProperties(lecturerViewVO, CourseLecturerResp.class));
        }
        
        return Result.success(experimentResp);
    }
    
    /**
     * 保存用户和实验的关系
     * 
     * @param req
     * @param userId
     * @return
     */
    public Result save(ExperimentReq req, Long userId) {
    	long experimentId = req.getId();
    	UserExperimentExample example = new UserExperimentExample();
    	example.createCriteria().andUserIdEqualTo(userId).andExperimentIdEqualTo(experimentId);
    	List<UserExperiment> list = userExperimentDao.listByExample(example);
        if (ObjectUtil.isEmpty(list)) {
        	UserExperiment userExperiment = new UserExperiment();
        	userExperiment.setUserId(userId);
        	userExperiment.setExperimentId(experimentId);
        	userExperimentDao.save(userExperiment);
//        } else {
//        	for (UserExperiment userExperiment : list) {
//        		userExperimentDao.updateById(userExperiment);
//			}
        }
        
        return Result.success("OK");
    }
    
    /**
     * 更新实验秘钥
     * 
     * @param req
     * @param userId
     * @return
     */
    public Result saveSecret(ExperimentReq req, Long userId) {
    	ExperimentWithBLOBs record = new ExperimentWithBLOBs();
    	record.setId(req.getId());
    	record.setSecret(req.getSecret());
    	dao.updateById(record);        
        
        return Result.success("OK");
    }
}
